// 把拿到的数据组装成路由数据
import React, { lazy, Suspense } from "react";


import APPOPTIONS from "@/app.config";
import { PageLoading } from "@ant-design/pro-components";

/**@description 动态导入所有页面组件 */
const dynamicImports = {
    HomeView: lazy(() => import("@/pages/WorkBench/workbench")),
    EchartView: lazy(() => import("@/pages/test/echartView/index")),
    AuthorityView: lazy(() => import("@/pages/users/authority")),
    BaiduMap: lazy(() => import("@/pages/test/BaiduMap/BaiduMap")),
    EditProduct: lazy(() => import("@/pages/product/editProduct/EditProduct")),
    DictView: lazy(() => import("@/pages/system/dict")),
    MenuView: lazy(() => import("@/pages/system/menu")),
    UserListView: lazy(() => import("@/pages/system/user")),
    ArticleListView: lazy(() => import("@/pages/context/article")),
    SubjectListView: lazy(() => import("@/pages/context/subject")),
    MessageListView: lazy(() => import("@/pages/context/message")),
    ProductListPages: lazy(() => import("@/pages/product/list")),
    CategoryListPages: lazy(() => import("@/pages/product/category")),
    ProductBrandList: lazy(() => import("@/pages/product/brand")),
    ProductClassList: lazy(() => import("@/pages/product/classType")),
    ProductPropertyList: lazy(() => import("@/pages/product/propertyList")),
    OrderList: lazy(() => import("@/pages/order/list")),
    EditArticle: lazy(() => import("@/pages/context/article/editArticle")),
    FileManagerList: lazy(() => import("@/pages/system/filemanager")),
    OrderDetail: lazy(() => import("@/pages/order/list/OrderDetail")),
    CodeGengeration: lazy(() => import("@/pages/development/codeGengeration")),
    PersonCenter: lazy(() => import("@/pages/system/personCenter/personCenter")),
    CustomerList: lazy(() => import("@/pages/custManager/custList/index")),
    DictDetail: lazy(() => import("@/pages/system/dict/data/index")),
    VipLevelView: lazy(() => import("@/pages/custManager/vipLevel/index")),
    VistorLogList: lazy(() => import("@/pages/webSite/VistorLogList/index")),
    ImageLibrary: lazy(() => import("@/pages/materialManager/imageLibrary/index")),
    BucketConfigList: lazy(() => import("@/pages/materialManager/bucketList/index")),
    DocumentLibrary: lazy(() => import("@/pages/materialManager/documentLibrary/index"))

}

/**@description 统一加载器 */
const DynamicLoader = ({ component: Component }) => {
    return (
        <Suspense fallback={<PageLoading />}>
            <Component />
        </Suspense>
    )
}


/**@description 固定路由的公用部分单独抽离出来 */
const commonMoreInfo = {
    menuType: "2",
    parentId: "0",
    menuIcon: "",
    sort: 1,
    visible: false,
    status: 1,
    children: []
}

/**
 * @param {activeMenu } 当前路由要激活哪个菜单？
 * @description 一些固定要用到的页面路由
 */
const remainingRouter = [

    {
        id: "context_type_edit_form",
        path: "/type/context/edit",
        menuName: "内容编辑",
        askBeforeClose: true, //关闭前询问
        activeMenu: '/context/article',
        routerPath: "article/edit",
        element: DynamicLoader({ component: dynamicImports.EditArticle }),
        componentName: "EditArticle",
        componentPath: "context/editArticle",
        permissionMark: "product:context:edit",
        ...commonMoreInfo
    },
    {

        id: "order_type_order_detail",
        path: "/order/list/orderDetail",
        menuName: "订单详情",
        activeMenu: '/order/list',
        componentName: "OrderDetail",
        componentPath: "order/list/OrderDetail",
        permissionMark: "order:list:detail",
        element: DynamicLoader({ component: dynamicImports.OrderDetail }),
        ...commonMoreInfo
    },
    {

        id: "system_type_user_center",
        path: "/system/person/center",
        menuName: "个人中心",
        activeMenu: '/system/personCenter',
        element: DynamicLoader({ component: dynamicImports.PersonCenter }),
        componentName: "PersonCenter",
        componentPath: "system/personCenter/personCenter",
        permissionMark: "system:personCenter:personCenter",
        ...commonMoreInfo
    },

]




/**@description 权限标识与页面组件map */
const componentMap = {
    "materialManager:storageBucket:list": <DynamicLoader component={dynamicImports.BucketConfigList} />,
    "custManager:vipLevel:list": <DynamicLoader component={dynamicImports.VipLevelView} />,
    "custManager:custList:list": <DynamicLoader component={dynamicImports.CustomerList} />,
    "system:filemanager:list": <DynamicLoader component={dynamicImports.FileManagerList} />,
    "order:list": <DynamicLoader component={dynamicImports.OrderList} />,
    "product:classProperty:list": <DynamicLoader component={dynamicImports.ProductPropertyList} />,
    "product:classType:list": <DynamicLoader component={dynamicImports.ProductClassList} />,
    "product:brand": <DynamicLoader component={dynamicImports.ProductBrandList} />,
    "product:category": <DynamicLoader component={dynamicImports.CategoryListPages} />,
    "test:apiConfig": <DynamicLoader component={dynamicImports.ApiConfigView} />,
    "test:source": <DynamicLoader component={dynamicImports.AssetsView} />,
    "test:echartMap": <DynamicLoader component={dynamicImports.EchartView} />,
    "test:baiduMap": <DynamicLoader component={dynamicImports.BaiduMap} />,
    "product:list": <DynamicLoader component={dynamicImports.ProductListPages} />,
    "context:subject:list": <DynamicLoader component={dynamicImports.SubjectListView} />,
    "context:message:list": <DynamicLoader component={dynamicImports.MessageListView} />,
    "context:article:list": <DynamicLoader component={dynamicImports.ArticleListView} />,
    "system:user:list": <DynamicLoader component={dynamicImports.UserListView} />,
    "system:role:list": <DynamicLoader component={dynamicImports.AuthorityView} />,
    "development:menu:list": <DynamicLoader component={dynamicImports.MenuView} />,
    "development:dict:list": <DynamicLoader component={dynamicImports.DictView} />,
    "product:list:edit": <DynamicLoader component={dynamicImports.EditProduct} />,
    "development:dict:data": <DynamicLoader component={dynamicImports.DictDetail} />,
    "home:dataBase": <DynamicLoader component={dynamicImports.HomeView} />,
    "development:codeGengeration:list": <DynamicLoader component={dynamicImports.CodeGengeration} />,
    "websiteManager:visotrLogList:list": <DynamicLoader component={dynamicImports.VistorLogList} />,
    "materialManager:imageLibrary:list": <DynamicLoader component={dynamicImports.ImageLibrary} />,
    "materialManager:documentLibrary:list": <DynamicLoader component={dynamicImports.DocumentLibrary} />
};





/** @description 组装路由 */
const parseRoutes = (list) => {
    // 使用 reduce 方法构建路由数组
    const buildRoutes = (routes) => routes.reduce((acc, item) => {
        const {
            id,
            parentId,
            activeMenu = undefined,
            visible = true,
            menuName,
            menuType,
            menuIcon,
            useKeepAlive = APPOPTIONS.KEEPALIVE,
            routerPath,
            sort,
            componentName,
            componentPath,
            permissionMark,
            status,
            children
        } = item;
        const route = {
            id,
            parentId,
            menuName,
            menuType,
            menuIcon,
            routerPath,
            sort,
            componentName,
            componentPath,
            permissionMark,
            status,
            activeMenu,
            visible,
            path: routerPath,
            element: componentMap[permissionMark] || null, // 使用映射对象获取组件
            children: children && children.length > 0 ? buildRoutes(children) : []
        };

        acc.push(route);
        return acc;
    }, []);

    // 构建主路由数组
    const mainRoutes = buildRoutes(list);

    // 合并其他路由
    return mainRoutes.concat(remainingRouter);
};

export default parseRoutes;